exports.AvatarUI = Animation.AASprite.subclass(
{
    classname: 'AvatarUI',
    
    initialize: function($super, model) 
    {
    	$super();
    	this._modelObj = model;
    	this._canChangeFloor = false;
    	this._canChangeFloorTimeout = null;
    	this._currentAvatarAction = null;
    	this._currentFloor = 0;
    	this._destinationFloor = -1;
    	this._pickSide = kSide.kSideNone; 
    },
	
	createView: function()
	{	
		this.addAvatarImage();
		this.handleAvatarActions();
		
		this._canChangeFloorTimeout = setTimeout(this.setCanChangeFloor.bind(this, true), 10000);
	},
	
	addAvatarImage: function()
	{
		var avatarImageBasePath = this.getRandomAvatarImage();
		var avatarImagePath = avatarImageBasePath + "1.png";	
		this.setImage(avatarImagePath, [TCUtil.getAdjustedX(25), TCUtil.getAdjustedY(50)], [0.5, 0.5]);
		
		//var basePath = "./Content/images/avatarimages/avatar2/";
		var animation = new GL2.Animation();
		for(var i = 1 ; i <= 8 ; i++ )
		{
			var animationFrame = new GL2.Animation.Frame(avatarImageBasePath + i + ".png", 200, [TCUtil.getAdjustedX(25), TCUtil.getAdjustedY(50)]);
			animation.pushFrame(animationFrame);			
		}
		animation.setLoopingEnabled(true);	
		this.setAnimation(animation);
	},
	
	getRandomAvatarImage: function()
	{
		var avatarType = Math.round(Math.random()*(NO_OF_AVATAR_IMAGES-1));
		
		var path = this.getBaseResourcePath()+"avatar"+avatarType+"/";
		return path;
	},
	
	setCanChangeFloor: function(value)
	{
		var totalFloors = MMGameLayer.getMallUI()._floorsUIArray.getLength();
		if(totalFloors > 1)
		{
			this._canChangeFloor = value;
		}
		else 
		{
			this._canChangeFloorTimeout = setTimeout(this.setCanChangeFloor.bind(this, true), 10000);
		}
	},
	
	handleAvatarActions: function()
	{
		var avatarAction = null;
		//(!TutorialManager.getHasCompletedTutorial() && TutorialManager.getCurrentTutorialStep() > 7)
		if(TutorialManager.getHasCompletedTutorial())
		{
			if(this._canChangeFloor)
			{
				avatarAction = Math.round(Math.random()*2);
				//console.log("Zulu: Change Floor Avatar Action "+avatarAction);
			}
			else
			{
				avatarAction = Math.round(Math.random()*1);
				//console.log("Zulu: Avatar Action "+avatarAction);
			}
		}
		else
		{
			if(this._canChangeFloor && TutorialManager.getCurrentTutorialStep() > 7)
			{
				avatarAction = Math.round(Math.random()*2);
			}
			else
			{
				avatarAction = Math.round(Math.random()*1);
			}
		}
		
		if(avatarAction == 0)
		{
			this._currentAvatarAction = kAvatarAction.kAvatarActionVisitStore;
		}
		else if(avatarAction == 1)
		{
			this._currentAvatarAction = kAvatarAction.kAvatarActionFreeLance;
		}
		else if(avatarAction == 2)
		{
			this._currentAvatarAction = kAvatarAction.kAvatarActionShiftFloor;
		}
		
		this.moveAvatarForCurrentAction();
	},
	
	moveAvatarForCurrentAction: function()
	{
		if(this._currentAvatarAction == kAvatarAction.kAvatarActionFreeLance)
		{
			this.moveFreeLance();
		} 
		
		else if(this._currentAvatarAction == kAvatarAction.kAvatarActionVisitStore)
		{
			//console.log("zaki::: going to call move to store ");
			this.moveToStore();
		}
		else 
		{
			this.goToLift();
		}
	},
	
	moveToStore: function() 
	{
		var storePosition = this.getRandomStorePosition();
		if(storePosition)
		{
			//this.stopAllActions();
			var offsetAdjustedX = TCUtil.getAdjustedX(35);
			
//			console.log("zaki::: in move to store going to call run action");
			var nextPoint = storePosition.getX() + offsetAdjustedX;
			var distanceToMove = Math.abs(this.getPosition().getX() - nextPoint);
			var time = distanceToMove/AVATAR_SPEED;
			
			//var threeDEffectY = TCUtil.getAdjustedY(10);
			var threeDEffectY = 0;
			
			this.setAvatarDirection(nextPoint);
			var newPoint = new Animation.AAMoveTo(new Core.Point(nextPoint, +(this.getPosition().getY()-threeDEffectY)), time);
			var moveToPoint = new Animation.AACallFuncND(this.fadeOutForStore.bind(this), null);
			
			var sequence = new Animation.AASequence(newPoint, moveToPoint);
			this.runAction(sequence);
		}
		else 
		{
			this.handleAvatarActions();
		}
	},
	
	
	
	goToStoreSuccessCB: function()
	{
		//console.log("zaki::: goToStoreSuccessCB");

		//var threeDEffectY = TCUtil.getAdjustedY(10);
		var threeDEffectY = 0;
		//this.setVisible(false);
		this.setPosition(this.getPosition().getX(), +(this.getPosition().getY()+ threeDEffectY));
		
		
		
		
		//////START FROM HERE TOMORROW
		//////START FROM HERE TOMORROW
		//////START FROM HERE TOMORROW
		//////START FROM HERE TOMORROW
		//////START FROM HERE TOMORROW
		//////START FROM HERE TOMORROW
		
		
		
		
		this._afterStoreDoneTimeout = setTimeout(this.fadeInForStore.bind(this), 7000);
	},
	
	moveOutFromStore: function()
	{
		//this.setVisible(true);
		//this.fadeInForStore();
		//temporary work
		//this.handleAvatarActions();//
		if(this.getAnimation())
		{
			this.getAnimation().setLoopingEnabled(true);
			this.setAnimation(this.getAnimation());
			this.moveFreeLance();
		}	
	},
	
	getRandomStorePosition: function()
	{
		var x = this._currentFloor;
		var y = Math.round(Math.random()*2);
		var storeUI = MMGameLayer.getMallUI().getStoreUIForGridPosition(x,y);
		var storePosition = null;
		if(storeUI)
		{
			storePosition = storeUI.getPositionRelevantToMall();
			//console.log("zaki::: store position is X: "+storePosition.getX()+"   Y: "+storePosition.getY());
		}
		return storePosition;
	},
	
	moveFreeLance: function()
	{
		var nextPoint = TCUtil.getAdjustedX(this.getNextMovePoint());
		var distanceToMove = Math.abs(this.getPosition().getX() - nextPoint);
		var time = distanceToMove/AVATAR_SPEED;
		this.setAvatarDirection(nextPoint);
		var newPoint = new Animation.AAMoveTo(new Core.Point(nextPoint, this.getPosition().getY()), time);
		var moveToPoint = new Animation.AACallFuncND(this.goToNextPointSuccessCB.bind(this), null);
		
		var sequence = new Animation.AASequence(newPoint, moveToPoint);
		this.runAction(sequence);
	},
	
	getNextMovePoint: function()
	{
		var randomPoint = Math.floor(Math.random()*330);
		return randomPoint;
	},
	
	goToNextPointSuccessCB: function()
	{
		this.handleAvatarActions();
	},
	
	setAvatarDirection: function(nextPoint)
	{
		if(this.getPosition().getX() < nextPoint)
		{
			this.setScale(new Core.Vector(1,1));
		}
		else 
		{
			this.setScale(new Core.Vector(-1,1));
		}
	},
	
	setCurrentFloor: function(currentFloor)
	{
		this._currentFloor = currentFloor;
	},
	
	getAvatarSourceFloor: function()
	{
		//var floor = Math.floor(Math.abs(this.getPosition().getY()/FLOOR_HEIGHT));
		//return floor;
		return this._currentFloor;
	},
	
	goToLift: function()
	{
		//this.stopAllActions();
		do
		{
			//console.log("Zulu:: In While");
			this._destinationFloor = this.generateDestinationFloor();
			
		} while(this.getAvatarSourceFloor() == this._destinationFloor)
		
		this._moveToLift();
	},
	
	getDestinationFloor: function()
	{
		return this._destinationFloor;
	},
			
	generateDestinationFloor: function()
	{
		var totalFloors = MMGameLayer.getMallUI()._floorsUIArray.getLength();
		//get visitors on destination floor here for better probability
		var destFloor = Math.floor(Math.random()*totalFloors); 

		return destFloor;
	},
	
	getRandomSide: function()
	{
		//console.log("Zulu: In getRandomSide");
		if(ElevatorHandler.getElevatorsUIArray().getLength() > 1)
		{
			//console.log("Zulu: Length > 1");
			var side = Math.random()*2;
			if(side > 1)
			{
				return kSide.kSideLeft;	
			}
			return kSide.kSideRight
		}
		else
		{
			//console.log("Zulu: Length Elseeeeeee");
			return kSide.kSideLeft;
		}
	},
	
	getPickSide: function()
	{
		return this._pickSide;
	},
	
	setPickSide: function(pickSide)
	{
		this._pickSide = pickSide;
	},	
	
	_moveToLift: function()
	{
		var moveSide = this.getRandomSide();
		this._pickSide = moveSide;
		var liftPoint = null;
		if(this._pickSide == kSide.kSideLeft)
		{
			liftPoint = TCUtil.getAdjustedX(45);
		}
		else 
		{
			liftPoint = TCUtil.getAdjustedX(280);
		}
		
		this.setAvatarDirection(liftPoint);
		
		var distanceToMove = Math.abs(this.getPosition().getX() - liftPoint);
		var time = distanceToMove/AVATAR_SPEED;
		
		var newPoint = new Animation.AAMoveTo(new Core.Point(liftPoint, this.getPosition().getY()), time);
		var moveToPoint = new Animation.AACallFuncND(this.reachedLiftCB.bind(this), null);
		
		var sequence = new Animation.AASequence(newPoint, moveToPoint);
		this.runAction(sequence);
	},
	
	reachedLiftCB: function()
	{
		if(this._pickSide == kSide.kSideLeft )
		{
			this.setScale(new Core.Vector(-1,1));
		}
		else 
		{
			this.setScale(new Core.Vector(1,1));
		}
		ElevatorHandler.addInterestedAvatar(this);
		if(this.getAnimation())
		{
			this.getAnimation().setLoopingEnabled(false);
		}
	},
	
	fadeOutForStore: function()
	{
		if(this.getAnimation())
		{
			var time = 1;
			var fadeOut = new Animation.AAFadeOut(time);
			var callback = new Animation.AACallFuncND(this.goToStoreSuccessCB.bind(this), null);
			this.getAnimation().setLoopingEnabled(false);
			var sequence = new Animation.AASequence(fadeOut, callback);
			this.runAction(sequence);
		}
	},
	
	fadeInForStore: function()
	{
		var time = 1;
		var fadeIn = new Animation.AAFadeIn(time);
		var callback = new Animation.AACallFuncND(this.moveOutFromStore.bind(this), null);
		
		var sequence = new Animation.AASequence(fadeIn, callback);
		this.runAction(sequence);		
	},
	
	removeAvatarFromFloor: function()
	{
		this.setVisible(false);
	},
			
	unloadAvatarAtFloor: function(floor)
	{
		if(this.getAnimation())
		{	
			this._currentFloor = this._destinationFloor;
			this._destinationFloor = -1;
			this._canChangeFloor = false;
			var y = TCUtil.getAdjustedY(-(35 + (floor) * FLOOR_HEIGHT));
			this.setPosition(this.getPosition().getX(), y);
			
			this.getAnimation().setLoopingEnabled(true);
			this.setAnimation(this.getAnimation());
			this.setVisible(true);
			
			this.handleAvatarActions();
			this._pickSide = kSide.kSideNone;
			this._canChangeFloorTimeout = setTimeout(this.setCanChangeFloor.bind(this, true), 10000);
		}
		else
		{
			console.log("zaki:::::: **************THIS(AVATARUI) IS NOT AN OBJECT DO SOMETHING************** :: "+this);
		}
	},
			
	getBaseResourcePath: function()
	{
		var tempPath = "./Content/images/avatarimages/"
		
		return tempPath;
	},
								   
	destroy: function($super)
    {
    	this.stopAllActions();
		if(this._canChangeFloorTimeout)
		{
			clearTimeout(this._canChangeFloorTimeout);
			this._canChangeFloorTimeout = null;
		}
		if(this._afterStoreDoneTimeout)
		{
			clearTimeout(this._afterStoreDoneTimeout);
			this._afterStoreDoneTimeout = null;
		}
		$super();
    },
});
